package cloud.take.off.module.common.page;

import cloud.take.off.utils.DateUtil;
import cn.hutool.core.util.StrUtil;

import java.lang.reflect.Field;
import java.math.BigDecimal;

/**
 * 说明：反射工具 创建人：hdu_2017 修改时间：2014年9月20日
 * 
 * @version
 */
public class ReflectHelper {
	/**
	 * 获取obj对象fieldName的Field
	 * 
	 * @param obj
	 * @param fieldName
	 * @return
	 */
	public static Field getFieldByFieldName(Object obj, String fieldName) {
		for (Class<?> superClass = obj.getClass(); superClass != Object.class; superClass = superClass
				.getSuperclass()) {
			try {
				return superClass.getDeclaredField(fieldName);
			} catch (NoSuchFieldException e) {
			}
		}
		return null;
	}

	/**
	 * 获取obj对象fieldName的属性值
	 * 
	 * @param obj
	 * @param fieldName
	 * @return
	 * @throws SecurityException
	 * @throws NoSuchFieldException
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 */
	public static Object getValueByFieldName(Object obj, String fieldName)
			throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
		Field field = getFieldByFieldName(obj, fieldName);
		Object value = null;
		if (field != null) {
			if (field.isAccessible()) {
				value = field.get(obj);
			} else {
				field.setAccessible(true);
				value = field.get(obj);
				field.setAccessible(false);
			}
		}
		return value;
	}

	/**
	 * 设置obj对象fieldName的属性值
	 * 
	 * @param obj
	 * @param fieldName
	 * @param value
	 * @throws SecurityException
	 * @throws NoSuchFieldException
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 */
	public static void setValueByFieldName(Object obj, String fieldName, Object value)
			throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
		Field field = obj.getClass().getDeclaredField(fieldName);
		if (field.isAccessible()) {
			field.set(obj, value);
		} else {
			field.setAccessible(true);
			field.set(obj, value);
			field.setAccessible(false);
		}
	}

	public static void setRPValueByFieldName(Object obj, String fieldName, String value)
			throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
		Field field;
		try {
			field = obj.getClass().getDeclaredField(fieldName);
		}catch (NoSuchFieldException e){
			return;
		}
		boolean flag = field.isAccessible();
		if (!flag) {
			field.setAccessible(true);
		}
		switch (field.getGenericType().getTypeName()){
			case "int":
			case "java.lang.Integer":
				field.set(obj, new Integer(value));
				break;
			case "short":
			case "java.lang.Short":
				field.set(obj, new Short(value));
				break;
			case "long":
			case "java.lang.Long":
				field.set(obj, new Long(value));
				break;
			case "float":
			case "java.lang.Float":
				field.set(obj, new Float(value));
				break;
			case "double":
			case "java.lang.Double":
				field.set(obj, new Double(value));
				break;
			case "boolean":
			case "java.lang.Boolean":
				field.set(obj, new Boolean(value));
				break;
			case "java.lang.String":
				field.set(obj, value);
				break;
			case "java.util.Date":
				if (StrUtil.isNotBlank(value)) {
					if (value.length() == 19) {
						field.set(obj, DateUtil.parse(value, DateUtil.PATTERN_DATETIME));
					} else if (value.length() == 7) {
						field.set(obj, DateUtil.parse(value, DateUtil.PATTERN_MONTH));
					} else if (value.length() == 4) {
						field.set(obj, DateUtil.parse(value, DateUtil.PATTERN_YEAR));
					} else if (value.length() == 10) {
						field.set(obj, DateUtil.parse(value, DateUtil.PATTERN_DATE));
					}
				}
				break;
			case "java.math.BigDecimal":
				field.set(obj, new BigDecimal(value));
				break;
			default:
				field.set(obj, value);
				break;
		}
		if (!flag){
			field.setAccessible(false);
		}
	}
}
